package com.hzw.saas.common.config.serializer.json;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
· * 序列化自动加密一半字符串
 */
@Component
public class StringEncryptSerializer extends JsonSerializer<String> {

    private final static String PREFIX = "*";

    @Override
    public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        if (StrUtil.isBlank(value)) {
            gen.writeString(StrUtil.EMPTY);
            return;
        }
        String backHalf = value.substring(value.length()/2);
        gen.writeString(PREFIX.concat(backHalf));
    }

}
